package com.frsf.controller;

import com.frsf.entity.Item;
import com.frsf.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author FRSF
 * @Date 2022/4/26 9:32
 * @Description
 **/
@Slf4j
@RestController
@RequestMapping(value = "/uptext")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @PostMapping
    public String postText(@RequestBody String[] items) {
        if (items.length != 0) {
            Item item1 = new Item();
            //  移除原有的数据库选项
            itemService.remove(null);
            for (String item : items) {
                item1.setItems(item);
                itemService.save(item1);
            }
            return "上传成功";
        }
        return "上传失败,数组当前长度为0";
    }

    @GetMapping
    public List<String> getText() {
        List<Item> list = itemService.list();
        List<String> items = new ArrayList<>();
        for (Item item : list)
            items.add(item.getItems());
        return items;
    }
}
